using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Web;

namespace TangoViewExtension
{
   public partial class RequestDisplayControl : UserControl
   {
      public RequestDisplayControl()
      {
         InitializeComponent();
      }

       public void Display(string str, string content_type)
       {
          try
          {
             TextDisplay(str);
             treeView1.Nodes.Clear();
             if (content_type.IndexOf("application/x-www-form-urlencoded")!=-1)
                 XmlFormDisplay(str);
             else
                 //TFS: application/soap+xml; charset=utf-8
                 //Displays nothing if fails to parse XML
                 XmlSimpleDisplay(str);
          }
          catch (Exception ex)
          {
             MessageBox.Show(ex.Message, "TangoView Error");
          }
       }

       private void XmlSimpleDisplay(string str)
       {
           if (null == str || "" == str)
               return;
           XmlDocument doc = new XmlDocument();
           try
           {
               doc.LoadXml(HttpUtility.UrlDecode(str));
               DisplayXmlRecursive(treeView1.Nodes, doc.ChildNodes);
           }
           catch
           {
               //failed to parse XML - display nothing               
           }
       }

       private void XmlFormDisplay(string str)
       {
           if (null == str || "" == str)
               return;
           ArrayList arr = ParseFormBody(str);

           foreach (string[] pair in arr)
           {
               TreeNode node = new TreeNode(HttpUtility.UrlDecode(pair[0]));               
               treeView1.Nodes.Add(node);
               XmlDocument doc = new XmlDocument();
               try
               {
                   doc.LoadXml(HttpUtility.UrlDecode(pair[1]));
                   DisplayXmlRecursive(node.Nodes, doc.ChildNodes);
               }
               catch
               {
                   TreeNode node2 = new TreeNode(HttpUtility.UrlDecode(pair[1]));
                   node2.Expand();
                   node.Nodes.Add(node2);
               }
               node.Expand();
           }
       }

       private void TextDisplay(string str)
       {
           if (null != str && ""!=str)
               txtBody.Text = HttpUtility.UrlDecode(str);
           else
               txtBody.Text = "";
       }

      private ArrayList ParseFormBody(string body)
      {
         string[] parameters = body.Split('&');
         ArrayList arr = new ArrayList();
         foreach (string str2 in parameters)
         {
            string[] pair = str2.Split('=');
            arr.Add(pair);
         }
         return arr;
      }

      private void DisplayXmlRecursive(TreeNodeCollection tree_nodes, XmlNodeList xml_nodes)
      {
         foreach (XmlNode xml_node in xml_nodes)
         {
            TreeNode tree_node = new TreeNode(MyNodeToString(xml_node));
            tree_nodes.Add(tree_node);
            DisplayXmlRecursive(tree_node.Nodes, xml_node.ChildNodes);
            tree_node.Expand();
         }
      }

      private string MyNodeToString(XmlNode node)
      {
         try
         {
            if (node is XmlDeclaration)
               return node.OuterXml;
            if (node is XmlText)
                return node.OuterXml;
            string res = "<" + node.Name;
            if (node.Attributes != null)
               foreach (XmlAttribute attr in node.Attributes)
                  res += " " + attr.Name + "='" + attr.Value + "'";
            res += " >";
            return res;
         }
         catch (Exception ex)
         {
            return node.Name + " " + ex.Message;
         }
      }

      private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
      {
         System.Diagnostics.Process.Start("mailto:thest2@gmail.com");
      }
   }
}
